
package com.test.view;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Vector;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import com.test.dao.CourseDao;
import com.test.model.Course;

/**
 * 课程管理的View
 * @author 23070
 *
 */
public class CourseView extends JFrame {

    public JPanel thePanel;

    private JPanel jPAddCourse;  //学生新增
    private JPanel jPCourseManager ; //学生管理

    private JPanel jPButton ; //按钮框

    //设置label
    private JLabel jLid_course;  //学生姓名
    private JTextField txtid_course; //学生姓名文本框
    private JLabel jLcourse_name;
    private JTextField txtcourse_name;
    private JLabel jLbefore_course;
    private JTextField txtbefore_course;
    private JLabel jLstart_term;
    private JTextField txtstart_term;
    private JLabel jLclass_hour;
    private JTextField txtclass_hour;

    private JLabel jLcredit;
    private JTextField txtcredit;

    //数据表
    private JTable jTCourse;

    //学生操作按钮
    private JButton jbAdd,jbEdit,jbDeleteSelected;  //新增，删除，删除选中项

    private JLabel jLFindKeyword;  //查找关键字
    private JTextField txtFindKeyword; //查找关键字
    private JComboBox jcbFindKeyword; //查找关键字
    private JLabel jLSortKeyword;  //排序关键字
    private JTextField txtSortKeyword; //查找关键字
    private JComboBox jcbSortKeyword; //查找关键字

    private List<Course> CourseList = new ArrayList<Course>();

    CourseDao courDao = new CourseDao() ;

    /** Creates new form MainFrm */
    public CourseView() {
        initComponents();
        this.setSize(800, 360);
        this.beginCourseList();
        this.fillTable();
    }

    /**
     * 页面初始话
     */
    private void initComponents() {

        jPAddCourse = new JPanel();
        jPCourseManager = new JPanel();

        jPButton = new JPanel();
        jPButton.setLayout(new GridLayout(3,2));
        thePanel = new JPanel();

        thePanel.setLayout(new GridLayout(1,2));
        thePanel.add(jPAddCourse);
        thePanel.add(jPCourseManager);

        thePanel.setSize(500,360);
        jPAddCourse.setSize(100,360);
        jPCourseManager.setSize(400,360);

        //增加新增学生的页面
        jLid_course = new JLabel("课程号");
        jLcourse_name = new JLabel("课程名");
        jLbefore_course = new JLabel("先修课程号");

        jLstart_term = new JLabel("--");
        jLclass_hour = new JLabel("--");
        jLcredit = new JLabel("学分");

        jLid_course.setHorizontalAlignment(JLabel.RIGHT);
        jLcourse_name.setHorizontalAlignment(JLabel.RIGHT);
        jLbefore_course.setHorizontalAlignment(JLabel.RIGHT);
        jLstart_term.setHorizontalAlignment(JLabel.RIGHT);
        jLclass_hour.setHorizontalAlignment(JLabel.RIGHT);
        jLcredit.setHorizontalAlignment(JLabel.RIGHT);

        jLid_course.setSize(40, 30);
        jLcourse_name.setSize(40, 30);
        jLbefore_course.setSize(40, 30);
        jLstart_term.setSize(40, 30);
        jLclass_hour.setSize(40, 30);
        jLcredit.setSize(40, 30);


        txtid_course = new JTextField();
        txtcourse_name = new JTextField();
        txtbefore_course = new JTextField();
        txtstart_term = new JTextField();
        txtclass_hour = new JTextField();
        txtcredit = new JTextField();

        txtid_course.setSize(70, 30);
        txtcourse_name.setSize(70, 30);
        txtbefore_course.setSize(70, 30);
        txtstart_term.setSize(70, 30);
        txtclass_hour.setSize(70, 30);
        txtcredit.setSize(70, 30);

        //将新增学生页面添加
        jPAddCourse.setLayout(new GridLayout(6,2));
        jPAddCourse.add(jLid_course);
        jPAddCourse.add(txtid_course);
        jPAddCourse.add(jLcourse_name);
        jPAddCourse.add(txtcourse_name);
        jPAddCourse.add(jLbefore_course);
        jPAddCourse.add(txtbefore_course);

        jPAddCourse.add(jLcredit);
        jPAddCourse.add(txtcredit);

        //数据查看页面
        jPCourseManager.setLayout(new GridLayout(3,1,0,0));

        //学生表
        jTCourse = new JTable();
        jTCourse.setModel(new DefaultTableModel(new Object[][] {}, new String[] { "课程号", "课程名", "先修课程号",
                "学分" }) {
            boolean[] canEdit = new boolean[] { false, false, false,  false };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });

        jTCourse.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                bookTypeTableMousePressed(evt);
            }
        });

        jTCourse.setVisible(true);
        jTCourse.setSize(400,40);

        JTableHeader jTableHeader = jTCourse.getTableHeader();
        jTableHeader.setSize(400,240);

        jPCourseManager.add(jTableHeader);
        jPCourseManager.add(jTCourse);
        jPCourseManager.setPreferredSize(new Dimension(300, 300));

        //设置操作按钮
        jbAdd = new JButton("添加");
        jbAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAdd(evt);
            }
        });

        //设置操作按钮
        jbEdit = new JButton("修改");
        jbEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbEdit(evt);
            }
        });

        jbDeleteSelected = new JButton("删除选中项");
        jbDeleteSelected.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbDeleteSelect(evt);
            }
        });

        jLFindKeyword = new JLabel("查找关键字");
        txtFindKeyword = new JTextField();
        jLSortKeyword = new JLabel("排序关键字");
        txtSortKeyword = new JTextField();

        jcbFindKeyword = new JComboBox();
        jcbFindKeyword.addItem("课程号");
        jcbFindKeyword.addItem("课程名");
        jcbFindKeyword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findEvent(evt);
            }
        });

        jcbSortKeyword = new JComboBox();
        jcbSortKeyword.addItem("课程号");
        jcbSortKeyword.addItem("课程名");
        jcbSortKeyword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sortEvent(evt);
            }
        });

        jPButton.add(jbAdd);
        jPButton.add(jbDeleteSelected);
        jPButton.add(jLFindKeyword);
        jPButton.add(jcbFindKeyword);
        jPButton.add(jbEdit);

        jPButton.setSize(400,80);

        jPCourseManager.add(jPButton);

        this.add(thePanel);
    }

    /*
     * 学生信息表里的鼠标监听
     * @param evt
     */
    private void bookTypeTableMousePressed(java.awt.event.MouseEvent evt) {
        // 获取选中的行
        int row = jTCourse.getSelectedRow();

        txtid_course.setText((String)jTCourse.getValueAt(row, 0));
        txtcourse_name.setText((String)jTCourse.getValueAt(row, 1));
        txtbefore_course.setText((String)jTCourse.getValueAt(row, 2));
        txtcredit.setText((String)jTCourse.getValueAt(row, 3));

    }

    /**
     * 添加按钮的鼠标监听
     * @param evt
     */
    private void jbAdd(java.awt.event.ActionEvent evt) {
        System.out.println("---添加按钮");

        String id_course =  " 0 " ;
        String course_name =  " 0 " ;
        String before_course=  " 0 " ;
        String start_term = " 0 " ;
        String class_hour = " 0 " ;
        String credit=  " 0 " ;

        id_course =  txtid_course.getText() ;
        course_name = txtcourse_name.getText() ;
        before_course= txtbefore_course.getText() ;

        credit= txtcredit.getText() ;  //学分

        Course stu = new Course(id_course, course_name, before_course, start_term, class_hour, credit);
        System.out.println("---Stu : " + stu.toString() );

        try {
            courDao.AddCourses(id_course, course_name, before_course, start_term, class_hour, credit);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //保存新增学生
        this.CourseList.add(stu);
        //修改学生列表
        this.fillTable();

    }

    /**
     * 添加按钮的鼠标监听
     * @param evt
     */
    private void jbEdit(java.awt.event.ActionEvent evt) {
        System.out.println("---添加按钮");

        String id_course =  txtid_course.getText() ;;
        String course_name = txtcourse_name.getText() ;;
        String before_course= txtbefore_course.getText() ;
        String start_term = " 0 " ;
        String class_hour = " 0 " ;
        String credit= txtcredit.getText() ;

        Course stu = new Course(id_course, course_name, before_course, start_term, class_hour, credit);
        System.out.println("---Stu : " + stu.toString() );

        try {
            courDao.UpdateCoursesById(id_course, course_name, before_course, start_term, class_hour, credit);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        List<Course> theCourseList = new ArrayList<Course>();


        int allrow = jTCourse.getRowCount();
        String stuName ;
        String birthday ;
        String sex ;
        String province = "0";
        String city = "0" ;
        String college  ;

        //将删除后的列表存储
        for(int i=0 ;i< allrow ;i++){
            stuName = (String)jTCourse.getValueAt(i, 0);   //姓名
            birthday = (String)jTCourse.getValueAt(i, 1);  //出生年月日
            sex = (String)jTCourse.getValueAt(i, 2);  //性别

            college  = (String)jTCourse.getValueAt(i, 3);  //学院

            stu = new Course(stuName, birthday, sex, province, city, college);

            if(id_course.equals(stuName )){
                ;
            } else {
                theCourseList.add(stu);
            }
        }

        stu = new Course(id_course, course_name, before_course, start_term, class_hour, credit);

        theCourseList.add(stu);

        //保存新增学生
        this.CourseList = theCourseList ;
        //修改学生列表
        this.fillTable();

    }

    /**
     * 删除选中项按钮的监听
     * @param evt
     */
    private void jbDeleteSelect(java.awt.event.ActionEvent evt) {
        System.out.println("---删除选中项");
        // 获取选中的行
        int row = jTCourse.getSelectedRow();
        int allrow = jTCourse.getRowCount();
        String stuName ;
        String birthday ;
        String sex ;
        String province = "0";
        String city = "0" ;
        String college  ;

        List<Course> theCourseList = new ArrayList<Course>();

        //将删除后的列表存储
        for(int i=0 ;i< allrow ;i++){
            stuName = (String)jTCourse.getValueAt(i, 0);   //姓名
            birthday = (String)jTCourse.getValueAt(i, 1);  //出生年月日
            sex = (String)jTCourse.getValueAt(i, 2);  //性别

            college  = (String)jTCourse.getValueAt(i, 3);  //学院

            Course stu = new Course(stuName, birthday, sex, province, city, college);

            if(i == row ){

                try {
                    courDao.DeleteCoursesById(stuName);
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                ;
            } else {
                theCourseList.add(stu);
            }
        }
        //新的学生列表覆盖原有学生列表
        this.CourseList = theCourseList ;

        //修改学生列表
        this.fillTable();
    }

    /**
     * 查找筛选框
     * @param evt
     */
    private void findEvent(java.awt.event.ActionEvent evt) {
        System.out.println("---查找筛选框");
        String findStr = (String) jcbFindKeyword.getSelectedItem();
        String sortStrName = "课程名" ;
        System.out.println("--筛选字段 " + findStr );

        int allrow = jTCourse.getRowCount();

        String stuName ;
        String birthday ;
        String sex ;
        String province ;
        String city ;
        String college  ;

        String thestuName = txtcourse_name.getText() ;  //姓名


        boolean selected = false ;

        List<Course> selectedCourseList = new ArrayList<Course>();


        for(Course stu : CourseList){
            stuName = stu.getCourse_name();   //姓名

            selected = false ;

            //判断按学生名称筛选还是按出生日期筛选
            if(sortStrName == findStr ){
                if(thestuName.equals(stuName)){
                    selected = true ;
                }
            }

            if(selected){
                selectedCourseList.add(stu);
            }
        }

        //将数据更新
        this.fillTable(selectedCourseList);
    }

    /**
     * 排序框
     * @param evt
     */
    private void sortEvent(java.awt.event.ActionEvent evt) {
        System.out.println("---排序选中项");
        String sortStr = (String) jcbSortKeyword.getSelectedItem();
        System.out.println("== " + sortStr);
        String sortStrName = "课程号" ;

        //进行排序操作

        if(sortStrName.equals(sortStr)){
            Collections.sort(this.CourseList,new Comparator<Course>() {
                @Override
                public int compare(Course o1, Course o2) {
                    return o1.getId_course().compareTo(o2.getId_course()) ;
                }
            });
        } else {
            Collections.sort(this.CourseList,new Comparator<Course>() {
                @Override
                public int compare(Course o1, Course o2) {
                    return o1.getCourse_name().compareTo(o2.getCourse_name()) ;
                }
            });
        }
        System.out.println(this.CourseList);

        //设置表单数据
        this.fillTable();
    }

    private void beginCourseList() {
        this.CourseList = new ArrayList<Course>();

        try {
            this.CourseList = courDao.getAllCourses();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置CompuerTable
     * @param bookType
     */
    private void fillTable() {
        DefaultTableModel dtm = (DefaultTableModel) jTCourse.getModel();
        dtm.setRowCount(0);
        try {

            for(Course stu : CourseList){
                //设置每一行
                Vector v = new Vector();
                v.add(stu.getId_course());
                v.add(stu.getCourse_name());
                v.add(stu.getBefore_course());
                v.add(stu.getCredit());
                dtm.addRow(v);
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {

        }
    }

    /**
     * 设置CompuerTable
     * @param bookType
     */
    private void fillTable(List<Course> theCourseList) {
        DefaultTableModel dtm = (DefaultTableModel) jTCourse.getModel();
        dtm.setRowCount(0);
        try {

            for(Course stu : theCourseList){
                //设置每一行
                Vector v = new Vector();
                v.add(stu.getId_course());
                v.add(stu.getCourse_name());
                v.add(stu.getBefore_course());

                v.add(stu.getCredit());
                dtm.addRow(v);
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {

        }
    }


    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CourseView().setVisible(true);
            }
        });
    }

}
